<?php
    class ValoracionRiesgoPacientesController extends AppController {
        var $name = 'ValoracionRiesgoPacientes';
        var $uses = array('ValoracionRiesgoPaciente');
        var $helpers = array('Javascript');
        var $components = array('AdminParameters');


        function  getList(){
            $this->render("list", "ajax");
        }

        function getListData($page=1, $limit=10, $orderColumn='fecha', $ascDesc='desc'){

            $result = array("data" => array(), "pageInfo" => array(
		    "total" => 0,
		    "page"  => 1,
		    "limit" => 1
		));



            $this->set("result", $result);

            $this->render("/elements/ajax_return", "ajax");

        }

        function getDetail($op, $id){

            $this->set("operation", $op);
            $this->set("editId", $id);

            $this->render("detalle_riesgos", "ajax");

        }

        function getDetailData($id){
            $data = $this->ValoracionRiesgoPaciente->find("all", array("conditions" => array("ValoracionRiesgoPaciente.id" => $id)));

            $this->loadModel("Riesgo");
            $idRiesgos = $this->Riesgo->find("list", array("fields" => array("Riesgo.id", "Riesgo.nombre")));

            $riesgos = array();
            $i=0;
            foreach($data as $rp){
                $riesgos[$i]["ValoracionRiesgoPaciente"] = $rp["ValoracionRiesgoPaciente"];
                foreach($idRiesgos as $idR => $nr){
                    $riesgos[$i]["ValoracionRiesgoPaciente"]["riesgo_".$idR] = "0";
                }
                foreach($rp["ValoracionRiesgo"] as $vr){
                    $riesgos[$i]["ValoracionRiesgoPaciente"]["riesgo_".$vr["riesgo_id"]] = "1";
                }
                $i++;
            }

            $this->set("result", array("data" => $riesgos));

            $this->render("/elements/ajax_return", "ajax");
        }

        function save(){

            $params = $this->params["form"];

            if($params["op"] == "add"){
                $this->__add($params);
            }
            if($params["op"] == "edit"){
                $this->__edit($params);
            }
            if($params["op"] == "del"){
                $this->__del($params);
            }

            $this->set("result", array("result" => true));           


            $this->render("/elements/ajax_return", "ajax");
        }

        function __edit($params){
            $rp = $this->ValoracionRiesgoPaciente->ValoracionRiesgo;

            $rp->deleteAll(array(
               "ValoracionRiesgo.valoracion_riesgo_paciente_id" => $params["id"]
            ));

            foreach($params as $key => $value){
                if(substr($key, 0, 7) == "riesgo_"){
                    $id = substr($key, 7, strlen($key));
                    if($params[$key] == "1"){
                        $arrSave = array(
                          "valoracion_riesgo_paciente_id" => $params["id"],
                          "riesgo_id" => $id
                        );
                        $rp->create($arrSave);
                        $rp->save();
                    }
                }
            }

            $this->ValoracionRiesgoPaciente->read(null, $params["id"]);

            $arrSave = array(
              "id" => $params["id"],
              "paciente_id" => $params["paciente_id"],
              "fecha" => $params["fecha"]
            );

            $this->ValoracionRiesgoPaciente->set($arrSave);
            $this->ValoracionRiesgoPaciente->save();

            return true;
        }

        function __add($params){
            $rp = $this->ValoracionRiesgoPaciente->ValoracionRiesgo;

            $arrSave = array(
              "paciente_id" => $params["paciente_id"],
              "fecha" => $params["fecha"]
            );

            $this->ValoracionRiesgoPaciente->create($arrSave);
            $this->ValoracionRiesgoPaciente->save();

            $idVRP = $this->ValoracionRiesgoPaciente->getInsertID();

            foreach($params as $key => $value){
                if(substr($key, 0, 7) == "riesgo_"){
                    $id = substr($key, 7, strlen($key));
                    if($params[$key] == "1"){
                        $arrSave = array(
                          "valoracion_riesgo_paciente_id" => $idVRP,
                          "riesgo_id" => $id
                        );

                        $rp->create($arrSave);
                        $rp->save();
                    }
                }
            }

            return true;

        }

        function __del($params){
            $rp = $this->ValoracionRiesgoPaciente->ValoracionRiesgo;

            $rp->deleteAll(array(
               "ValoracionRiesgo.valoracion_riesgo_paciente_id" => $params["id"]
            ));

            $this->ValoracionRiesgoPaciente->delete($params["id"]);
        }

        function getEvaluacionesPaciente($idPaciente){
            $data = $this->ValoracionRiesgoPaciente->find(
                    "list",
                    array(
                        "fields" => array("ValoracionRiesgoPaciente.id", "ValoracionRiesgoPaciente.fecha"),
                        "conditions" => array("ValoracionRiesgoPaciente.paciente_id" => $idPaciente),
                        "order" => array("ValoracionRiesgoPaciente.fecha DESC")
                        )
                    );

            $this->set("result", array("data" => $data));

            $this->render("/elements/ajax_return", "ajax");
        }

    }
?>